const txtLoginId =  new FieldValidator('txtLoginId',async function(val){
    if(!val){
        return '请输入账号'
    }
    const resp = await API.exists(val)
    if(resp.data){
        return '该账号已存在，请重新申请'
    }
})

const txtNickname = new FieldValidator('txtNickname',function(val){
    if(!val){
        return '请输入昵称'
    }
})
const loginPwd = new FieldValidator('txtLoginPwd',function(val){
    if(!val){
        return '请输入密码'
    }
})
const txtLoginPwdConfirm = new FieldValidator('txtLoginPwdConfirm',function(val){
    if(!val){
        return '请输入第二次密码'

    }
    if(val !== loginPwd.input.value){
        return '两次密码输入不一致'
    }
})

const form = $(".user-form");

form.addEventListener('submit',async function(e){
    e.preventDefault()
     const result = await  FieldValidator.validator(
        txtLoginId,
        txtNickname,
        loginPwd,
        txtLoginPwdConfirm,
    )
    if(!result){
        return 
    }

    const fromdata = new FormData(form);
    const data = Object.fromEntries(fromdata.entries())  
    const reg = await API.reg(data);
    if(reg.code === 0){
        alert('注册成功！')
        location.href = './login.html'
    }
    
})

// function test(){
//      FieldValidator.validator(txtLoginId,txtNickname,loginPwd,txtLoginPwdConfirm).then((result)=>{
//         console.log(result)
//      })
// }